'use strict';

angular.module('CBO').controller('TerminalController',
    ['$scope', '$stateParams', 'BaseModule', 'BaseFunction', 'CBOEnum', 'BaseService', '$location', 'Authentication',
    function($scope, $stateParams, BaseModule, BaseFunction, CBOEnum, BaseService, $location, Authentication) {
        $scope.TerminalTypeEnum = BaseFunction.getEnum('CBOEnum.TerminalType');
        $scope.btnSave = function(){
            if($scope.Module.Terminal.FocusedRecord.Code == undefined || 
                $scope.Module.Terminal.FocusedRecord.Code.trim() == ""){
                BaseFunction.alert("编码不能为空！", "保存提示");
                return;
            }
            if($scope.Module.Terminal.FocusedRecord.Name == undefined || 
                $scope.Module.Terminal.FocusedRecord.Name.trim() == ""){
                BaseFunction.alert("名称不能为空！", "保存提示");
                return;
            }
            if($scope.Module.Terminal.FocusedRecord.UniqueID == undefined || 
                $scope.Module.Terminal.FocusedRecord.UniqueID.trim() == ""){
                BaseFunction.alert("唯一标识不能为空！", "保存提示");
                return;
            }

            $scope.Module.Terminal.save();
        };
        $scope.btnCancel = function(){
            $scope.Module.Terminal.cancel();            
        };
        $scope.btnAdd = function(){
            $scope.Module.Terminal.add();
            $scope.Module.Terminal.FocusedRecord.IsEffective = true;
        };
        $scope.btnDelete = function(){
            $scope.Module.Terminal.delete();
        };
        $scope.btnList = function(){
            $location.url('/index/Terminal/List');
        };

        //初始化
        BaseModule.initModule($scope, 'Terminal');

        $scope.Module.Terminal.AfterOnAdd = function(record){
            record.SourceType = CBOEnum.TerminalSourceType.System.Value;
        };

        if($stateParams.id) {
            $scope.Module.Terminal.findById($stateParams.id, function(record){
                $scope.Module.Terminal.FocusedRecord = record;
            });
        }
        else{
            $scope.btnAdd();
        };
    }
]);
